function [schedTrnD1B, schedPosD1B, schedFBD1B, nTrialD1B, ...
          schedTrnD2P, schedPosD2P, schedFBD2P, nTrialD2P, ...
          schedTrnD2T, schedPosD2T, schedFBD2T, nTrialD2T, ...
          schedTrnD3T, schedPosD3T, schedFBD3T, nTrialD3T] = genSched(grp)

% Test TortoiseSVN!! :)
% Initialize random sequences
% RandStream.setDefaultStream(RandStream('mt19937ar','seed',sum(100*clock)));
rand ('state', time);


%% Day 1 baseline session: in which subjects learn the default mapping 
% between the hand posture cursor movements for 8 target positions
% separated to each other by 45 deg. This is the same across all groups.
nTrialD1B = 200;
schedTrnD1B = ones(1, nTrialD1B);
schedPosD1B = [];
for iTr = 1:8:nTrialD1B
    schedPosD1B = [schedPosD1B (randperm(8)-1)*pi/4];
end
schedFBD1B = ones(1, nTrialD1B);

%% Day 2 practice sesseion 
% This starts with the same baseline trials as day 1, but follows
% by group specific practice trials

% Baseline trials
nTrialBL = 40;
schedTrnBL = ones(1, nTrialBL);
schedPosBL = [];
for iTr = 1:8:nTrialBL
    schedPosBL = [schedPosBL (randperm(8)-1)*pi/4];
end
schedFBBL = ones(1, nTrialBL)*0.5;

% Day 2 practice session: practice 3 targets
nTrialPR = 120;
schedFBPR = ones(1,nTrialPR)*0.5;	% show feedback only for the first half
rndoff = floor(rand(1)*12)*30;
targets = mod([0, 90, 120] + rndoff, 360);

if grp <= 3
    % Group 1, 2, and 3 practice one of scaling, rotation, and 
    % flipping tasks in the order of
    % practice-baseline-practice-baseline-practice
    schedTrnPR = [(grp+1)*ones(1,30), ones(1,15), (grp+1)*ones(1,30), ones(1,30), (grp+1)*ones(1,15)];
    schedPosPR = [];
    for iTr = 1:3:nTrialPR
        schedPosPR = [schedPosPR targets(randperm(3))*pi/180];
    end
    
elseif grp <= 5
    % Group 4 and 5 practice two tasks (scaling + either rotation
    % of flipping) in the alternating schedule
    schedTrnPR = reshape([2*ones(1,nTrialPR/2); (grp-1)*ones(1,nTrialPR/2)],1,nTrialPR);
    schedPosPR1 = [];
    schedPosPR2 = [];
    for iTr = 1:3:nTrialPR/2
        schedPosPR1 = [schedPosPR1 targets(randperm(3))];
        schedPosPR2 = [schedPosPR2 targets(randperm(3))];
    end
    schedPosPR = reshape([schedPosPR1; schedPosPR2],1,nTrialPR)*pi/180;

elseif grp <= 7
    % Group 6 and 7 practice two tasks (scaling + either rotation
    % of flipping) in the block schedule
    schedTrnPR = [2*ones(1,nTrialPR/2), (grp-3)*ones(1,nTrialPR/2)];
    schedPosPR = [];
    for iTr = 1:3:nTrialPR
        schedPosPR = [schedPosPR targets(randperm(3))*pi/180];
    end
    
else
    % Group 8 and 9 practice two tasks (scaling + either rotation
    % of flipping) in the optimal schedule
    % TODO: 
%     schedTrnPR = reshape([2*ones(1,nTrialPR/2); (grp-5)*ones(1,nTrialPR/2)],1,nTrialPR);
%     schedPosPR1 = [];
%     schedPosPR2 = [];
%     for iTr = 1:3:nTrialPR/2
%         schedPosPR1 = [schedPosPR1 targets(randperm(3))];
%         schedPosPR2 = [schedPosPR2 targets(randperm(3))];
%     end
%     schedPosPR = reshape([schedPosPR1; schedPosPR2],1,nTrialPR)*pi/180;
end

schedTrnD2P = [schedTrnBL schedTrnPR];
schedPosD2P = [schedPosBL schedPosPR];
schedFBD2P  = [schedFBBL schedFBPR];
nTrialD2P = nTrialBL + nTrialPR;

%% Day 2 test session
% this is the generalization test for group 1~3 and retention test for
% group 4~9
if grp <= 3
    nTrialD2T = 48;
    genTargets = mod([0,30,60,90,120,150,180,240,270,315] + rndoff, 360);
    schedTrnD2T = (grp+1)*ones(1,nTrialD2T);
    schedPosD2T = [];
    schedFBD2T  = [];
    for i = 1:3
        idx = randperm(length(genTargets));
        schedPosD2T = [schedPosD2T, genTargets(idx(1:3)), targets(randperm(3)), ...
                                    genTargets(idx(4:8)), targets(randperm(3)), ...
                                    genTargets(idx(9:10))];
        schedFBD2T = [schedFBD2T, zeros(1,3), ones(1,3), zeros(1,5), ones(1,3), zeros(1,2)];
    end
    schedPosD2T = schedPosD2T*pi/180;
else
    nTrialD2T = 60;
    schedFBD2T = zeros(1,nTrialD2T);
    if (grp == 4 || grp == 6 || grp == 8)
        schedTrnD2T = [2*ones(1,15), 3*ones(1,15), 2*ones(1,15), 3*ones(1,15)];
    else
        schedTrnD2T = [2*ones(1,15), 4*ones(1,15), 2*ones(1,15), 4*ones(1,15)];
    end
    schedPosD2T = [];
    for i = 1:3:nTrialD2T
        schedPosD2T = [schedPosD2T, targets(randperm(3))*pi/180];
    end
end

%% Day 3 test session
% this is the retention test only for group 4~9
schedTrnD3T = [];
schedPosD3T = [];
schedFBD3T = [];
nTrialD3T = 0;

if grp > 3
    nTrialD3T = 60;
    schedFBD3T = zeros(1,nTrialD3T);
    if (grp == 4 || grp == 6 || grp == 8)
        schedTrnD3T = [2*ones(1,15), 3*ones(1,15), 2*ones(1,15), 3*ones(1,15)];
    else
        schedTrnD3T = [2*ones(1,15), 4*ones(1,15), 2*ones(1,15), 4*ones(1,15)];
    end
    schedPosD3T = [];
    for i = 1:3:nTrialD3T
        schedPosD3T = [schedPosD3T, targets(randperm(3))*pi/180];
    end
end